<?php

class ResourcesModel extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function add_resource($data){
		$fileName = $data['resourcePath'];
		$sql = 'select count(*) as count from resources where resourcePath = ? and flag = 0';
		$result = $this->db->query($sql,array($fileName));
		$row = $result->row_array();
		if ($row['count'] == 0){
			$this->db->insert('resources',$data);
			return true;
		}else{
			return false;
		}
		
	}

	public function get_res_by_id($id){
		$sql = 'select * from resources where id=?';
		$result = $this->db->query($sql,array($id));
		return $result->row_array();
	}

	public function get_resources_by_name($userName){
		$sql = 'select * from resources where userName = ? and flag = 0';
		$result = $this->db->query($sql,array($userName));
		// $arr = array();
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	public function get_resource_num($userName){
		$sql = 'select count(*) as count from resources where userName = ? and flag = 0';
		$result = $this->db->query($sql,array($userName));
		$row = $result->row_array();
		return $row['count'];
	}
	//根据资源ID删除资源
	public function delete_resource($resourceId){
		$sql = 'delete from resources where id = ?';
		$result = $this->db->query($sql,array($resourceId));
		return $result;
	}
	//根据用户名删除回收站资源
	public function delete_resource_by_username($userName){
		$sql = 'delete from resources where userName=? and flag=1';
		$result = $this->db->query($sql,array($userName));
		return $result;
	}
	//根据文件路径删除回收站资源
	public function delete_resource_by_path($resourcePath){
		$sql = 'delete from resources where resourcePath=? and flag=1';
		$result = $this->db->query($sql,array($resourcePath));
		return $result;
	}

	public function get_resource_path($resourceId){
		$sql = 'select resourcePath from resources where id=? and flag = 0';
		$result = $this->db->query($sql,array($resourceId));
		return $result->row_array();
	}
	//根据回收站资源ID获取该资源的路径
	public function get_recycle_path($resourceId){
		$sql = 'select resourcePath from resources where id=? and flag = 1';
		$result = $this->db->query($sql,array($resourceId));
		return $result->row_array();
	}

	//根据资源ID获取资源
	public function get_resource_by_id($resourceId){
		$sql = 'select * from resources where id=?';
		$result = $this->db->query($sql,array($resourceId));
		return $result->row_array();
	}

	public function get_resource_name($resourceId){
		$sql = 'select resourceName from resources where id=? and flag = 0';
		$result = $this->db->query($sql,array($resourceId));
		return $result->row_array();
	}

	public function get_resource_by_type($userName,$type){
		$sql = 'select * from resources where userName = ? and resourceType = ? and flag = 0';
		$result = $this->db->query($sql,array($userName,$type));
		return $result->result_array();
	}

	public function get_resource_num_by_type($userName,$type){
		$sql = 'select count(*) as count from resources where userName=? and resourceType=? and flag = 0';
		$result = $this->db->query($sql,array($userName,$type));
		$row = $result->row_array();
		return $row['count'];
	}

	public function alter_resource($resourceId,$flag,$deleteTime){
		$sql = 'update resources set flag = ?, deleteTime = ? where Id = ?';
		$result = $this->db->query($sql,array($flag,$deleteTime,$resourceId));
		return $result;
	}
	//获得该用户回收站的资源
	public function get_recycle_resource($userName){
		$sql = 'select * from resources where userName = ? and flag = 1';
		$result = $this->db->query($sql,array($userName));
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	//获得该用户回收站资源路径
	public function get_recycle_resource_path($userName){
		$sql = 'select resourcePath from resources where userName=? and flag=1';
		$result = $this->db->query($sql,array($userName));
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	
	//获得该用户回收站资源的数目
	public function get_recycle_resource_num($userName){
		$sql = 'select count(*) as count from resources where userName = ? and flag = 1';
		$result = $this->db->query($sql,array($userName));
		$row = $result->row_array();
		return $row['count'];
	}

	//判断回收站相同资源的个数
	public function check_recycle_path($resourcePath){
		$sql = 'select count(*) as count from resources where resourcePath=?';
		$result = $this->db->query($sql,array($resourcePath));
		$row = $result->row_array();
		if($row['count'] > 1){
			return false;
		}else{
			return true;
		}
	}

	//查找该资源是否已存在于私有云
	public function is_resource_exist($resourcePath){
		$sql = 'select count(*) as count from resources where resourcePath=? and flag=0';
		$result = $this->db->query($sql,array($resourcePath));
		$row = $result->row_array();
		if($row['count'] > 0){
			return false;
		}else{
			return true;
		}
	}

	//把回收站的资源还原
	public function undo_resource($resourceId,$flag,$deleteTime){
		$sql = 'update resources set flag = ?, deleteTime = ? where Id = ?';
		$result = $this->db->query($sql,array($flag,$deleteTime,$resourceId));
		return $result;
	}

	

	//根据用户名和关键字查询资源
	public function get_search_resources($userName,$content){
		$sql = 'select * from resources where userName = ? and resourceName like ? and flag = 0';
		$result = $this->db->query($sql,array($userName,"%".$content."%"));
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
		//根据资源ID获取资源所属的用户名
	public function get_username_by_resid($resource_id){
		$sql = 'select userName from resources where id=?';
		$result = $this->db->query($sql,array($resource_id));
		$row = $result->row_array();
		return $row['userName'];
	}

}